<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="样本类型" prop="sampTypeId">
        <treeselect v-model="queryParams.sampTypeId" :normalizer="normalizerSampType" :options="sampleTypeOptions"
                    placeholder="请选择样本类型" style="width: 214px"
        />
      </el-form-item>
      <el-form-item label="预警类型" prop="sampAlarmType">
        <el-select
          v-model="queryParams.sampAlarmType"
          clearable
          placeholder="预警类型"
          size="small"
          style="width: 214px"
        >
          <el-option
            v-for="dict in sampAlarmOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="sampleAlarmList" size="small">
      <el-table-column align="center" label="样本复份编号" min-width="100" prop="sampBackupCode"/>
      <el-table-column align="center" label="样本类型" prop="sampTypeName"/>
      <el-table-column align="center" label="样本源编号" prop="sampSourceCode"/>
      <el-table-column align="center" label="样本源名称" prop="sampSourceName"/>
      <el-table-column align="center" label="采样时间" prop="samplingTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.samplingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="样本位置" prop="sampLocation" width="280"/>
      <el-table-column :formatter="sampStateFormat" align="center" label="样本状态" prop="sampState"/>
      <el-table-column align="center" label="冻融次数" prop="sampFreezeThawCount"/>
      <el-table-column align="center" label="条码/二维码" prop="qrCode"/>
      <el-table-column align="center" label="保质天数" prop="sampTypeShelfLife"/>
      <el-table-column :formatter="sampAlarmFormat" align="center" label="预警类型" prop="sampAlarmType"/>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listSampleAlarm } from '@/api/biobank/sampleAlarm'
import { listSampleType } from '@/api/biobank/sampleType'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'SampleAlarm',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样本预警 表格数据
      sampleAlarmList: [],
      // 样本状态
      sampStateOptions: [],
      // 样本预警类型
      sampAlarmOptions: [],
      // 样本类型 树选项
      sampleTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampBackupId: undefined,
        sampTypeId: undefined,
        sampAlarmType: undefined
      }
    }
  },
  created() {
    this.getDicts('sample_state').then(response => {
      this.sampStateOptions = response.data
    })
    this.getDicts('sample_alarm_type').then(response => {
      this.sampAlarmOptions = response.data
    })
    this.getList()
    this.getSampleTypeList()
  },
  methods: {
    /** 查询样本预警 列表 */
    getList() {
      this.loading = true
      listSampleAlarm(this.queryParams).then(response => {
        this.sampleAlarmList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 样本状态状态字典翻译 */
    sampStateFormat(row, column) {
      return this.selectDictLabel(this.sampStateOptions, row.sampState)
    },
    /** 样本预警类型字典翻译 */
    sampAlarmFormat(row, column) {
      return this.selectDictLabel(this.sampAlarmOptions, row.sampAlarmType)
    },
    /** 查询样本类型 列表 */
    getSampleTypeList() {
      listSampleType().then(response => {
        this.sampleTypeOptions = this.handleTree(response.data, 'sampTypeId', 'parentId')
      })
    },
    /** 转换样本类型 数据结构 */
    normalizerSampType(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.sampTypeId,
        label: node.sampTypeName,
        children: node.children
      }
    }
  }
}
</script>
